﻿--[[
   @brief    ui logic for game_topic_detail ui
   @author   saintzhao
   @version  1.0
   @date     2016-01-07
--]]

game_topic_detail_view = {
    current_height = 1024;
    current_width = 768;

    show_browser = function(show, title)
        local title_ctrl = ui_utility.get_ctrl_ptr_by_cid(CID_GAME_TOPIC_DETAIL_TITLE);
        local const_ui_str, str_holder = U8toCUIStr(title);
        title_ctrl:SetText(const_ui_str);
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_GAME_TOPIC_DETAIL_DLG);
        component:Show(show);
        if show == 1 then
            component:Restore();
            component:BringToTop();
        end
    end;

    close = function(uilib, dwID_Click)
        game_topic_detail_view.current_height = 1024;
        game_topic_detail_view.current_width = 768;
        local uilib = ui_utility.uilib();
        local id = uilib:GetControlIDbyCustomID(CID_GAME_TOPIC_DETAIL_DLG);
        layout_mgr.delay_remove_ctrl(id);
    end;

    minimize = function(uilib, dwID_Click)
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_GAME_TOPIC_DETAIL_DLG);
        component:Minimize();
    end;

    on_win_status = function(is_maximized_state)
        local prestore_btn = ui_utility.get_ctrl_ptr_by_cid(CID_GAME_TOPIC_DETAIL_RESTORE_BTN);
        local pmax_btn = ui_utility.get_ctrl_ptr_by_cid(CID_GAME_TOPIC_DETAIL_MAX_BTN);
        if prestore_btn == nil or pmax_btn == nil then
            return;
        end;

        if is_maximized_state then
            prestore_btn:Show(TRUE);
            pmax_btn:Show(FALSE);
        else
            prestore_btn:Show(FALSE);
            pmax_btn:Show(TRUE);
        end;
    end;

    on_restore = function(uilib, dwID_Click)
        game_topic_detail_view.on_win_status(false);
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_GAME_TOPIC_DETAIL_DLG);
        component:Restore();
        game_topic_detail_view.on_pos_change();
    end;

    on_maximize = function(uilib, dwID_Click)
        game_topic_detail_view.on_win_status(true);
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_GAME_TOPIC_DETAIL_DLG);
        component:Maximize();
        game_topic_detail_view.on_pos_change();
    end;
    
    on_pos_change = function()
        local pDlg = ui_utility.get_ctrl_ptr_by_cid(CID_GAME_TOPIC_DETAIL_DLG);
        if pDlg == nil then
            return;
        end;
        local dlg_rc = pDlg:GetRectEx(FALSE,TRUE);
        local dlg_width = dlg_rc.right - dlg_rc.left;
        local dlg_height = dlg_rc.bottom - dlg_rc.top;

        local pBtnGroupCtn = ui_utility.get_ctrl_ptr_by_cid(CID_GAME_TOPIC_DETAIL_BTN_GROUP);
        if pBtnGroupCtn ~= nil then
            local btn_group_rc = pBtnGroupCtn:GetRectEx(FALSE,TRUE);
            btn_group_rc.left = dlg_width - (btn_group_rc.right - btn_group_rc.left);
            btn_group_rc.right = dlg_width;
            pBtnGroupCtn:MoveEx(btn_group_rc,TRUE);
        end;

        local pweb = ui_utility.get_ctrl_ptr_by_cid(CID_GAME_TOPIC_DETAIL_QBWEB);
        if pweb ~= nil then
            local web_rc = pweb:GetRectEx(FALSE,TRUE);
            web_rc.left = 5;
            web_rc.right = dlg_width - 5;
            web_rc.top = 35;
            web_rc.bottom = dlg_height - 5;
            pweb:SetSize(web_rc);
        end;
    end;
    
    navigate = function(url, width, height, have_scroll)
        game_topic_detail_view.fit_size(url, width, height);
        base_utility.log("[game_topic_detail_view]navigate:" .. url);
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_GAME_TOPIC_DETAIL_QBWEB);
        local const_ui_str, str_holder = U8toCUIStr(url);
        game_topic_detail_view.set_scroll(control_id, have_scroll);
        component:Navigate(const_ui_str);
    end;

    create_browser = function()
        ierd_tgp.tpf_ui.Log("[game_topic_detail_view]create_browser.");
        layout_mgr.restore_layout("GameTopicDetail.tpf");
    end;

    set_size = function(width, height)
        base_utility.log("[game_topic_detail_view]set_size:" .. tostring(width) .. "," .. tostring(height));
        game_topic_detail_view.current_width = width;
        game_topic_detail_view.current_height = height;
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_GAME_TOPIC_DETAIL_DLG);
        local rc = component:GetRectEx(1,TRUE);
        rc.left = rc.left + (rc.right-rc.left-width)/2;
        rc.top = rc.top + (rc.bottom-rc.top-height)/2;
        rc.right = rc.left + width + 4;
        rc.bottom = rc.top + height + 32;
        component:MoveEx(rc,TRUE);
        local new_rc = rc;
        component = ui_utility.get_ctrl_ptr_by_cid(CID_GAME_TOPIC_DETAIL_QBWEB);
        rc.left = 2;
        rc.top = 30;
        rc.right = 2 + width;
        rc.bottom = 30 + height;
        component:SetSize(rc);
        
        local close_btn = ui_utility.get_ctrl_ptr_by_cid(CID_GAME_TOPIC_DETAIL_CLOSE_BTN);
        local btn_rc = close_btn:GetRectEx(0,TRUE);
        btn_rc.right = new_rc.right - new_rc.left;
        btn_rc.left = btn_rc.right - 38;
        close_btn:MoveEx(btn_rc,TRUE);
        local min_btn = ui_utility.get_ctrl_ptr_by_cid(CID_GAME_TOPIC_DETAIL_MIN_BTN);
        btn_rc = min_btn:GetRectEx(0,TRUE);
        btn_rc.right = new_rc.right - new_rc.left - 36;
        btn_rc.left = btn_rc.right - 38;
        min_btn:MoveEx(btn_rc,TRUE);
        local title = ui_utility.get_ctrl_ptr_by_cid(CID_GAME_TOPIC_DETAIL_TITLE);
        local title_rc = title:GetRectEx(0,TRUE);
        local title_width = title_rc.right - title_rc.left;
        title_rc.left = (new_rc.right - new_rc.left - title_width)/2;
        title_rc.right = title_rc.left + title_width;
        title:MoveEx(title_rc,TRUE);
    end;

    OnJsCallback = function(uilib, fuc_name, json)
        if fuc_name == "close" then
            game_topic_detail_view.close();
        end    
    end;

    OnTitieChanged = function(uilib, title)
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_GAME_TOPIC_DETAIL_TITLE);
        local const_ui_str, str_holder = U8toCUIStr(title);
        component:SetText(const_ui_str);
    end;

    fit_size = function(url, width, height)
        if width == nil or height ==nil then
            local width_str = web_utility.get_param_from_url(url, "bwidth");
            local height_str = web_utility.get_param_from_url(url, "bheight");
            if width_str == nil then
                return;
            end
            if height_str == nil then
                return;
            end
            width = tonumber(width_str);
            height = tonumber(height_str);
        end
        if width == game_topic_detail_view.current_width then
            if height == game_topic_detail_view.current_height then
                return;
            end
        end
        game_topic_detail_view.set_size(width, height);
    end;
    
    set_scroll = function(id, have_scroll)
        if id == nil or have_scroll == nil then
            return;
        end
        if have_scroll == 0 then
            ui_utility.setproperty_by_id(id, "have_scroll_", "false");
        end
    end;
};